﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace ArchSoft
{
    public partial class AddContactMail : Form
    {
        DataTable dt = new DataTable("ContactLst");
        DataSet ds = new DataSet();


       // string path = @"D:\SideDB\MailApp\Contact.scr";
        private string _Name;
        private string _Company;
        private string _Telephone;
        private string _Email;
        private string _projectPath;


        private bool Mod = false;

        public string ProjectPath
        {
            set { _projectPath = value; }
        }

        public bool Modify
        {
            set { Mod = value; }
        }

        public string NameHHH
        {
            set { _Name = value; }
        }

        public string Company
        {
            set { _Company = value; }
        }

        public string Telephone
        {
            set { _Telephone = value; }
        }

        public string Email
        {
            set { _Email = value; }
        }

        public AddContactMail()
        {
            InitializeComponent();

            dt.Columns.Add("Name");
            dt.Columns.Add("Company");
            dt.Columns.Add("Telephone");
            dt.Columns.Add("Email");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!Mod)
            {
                bool bTest = ValidateEmailAddress(txtEmail.Text);

                if (bTest == false)
                    //MessageBox.Show("Invalid recipient email address: " + txtEmail.Text);
                    MessageBox.Show("Địa chỉ email người nhận không hợp lệ" + txtEmail.Text);

                DataRow row = dt.NewRow();
                row["Name"] = txtName.Text;
                row["Company"] = txtCompany.Text;
                row["Telephone"] = txtPhone.Text;
                row["Email"] = txtEmail.Text;

                dt.Rows.Add(row);
            }
            else
            {
                string email = _Email;
                DataSet ds1 = new DataSet();
                string path = _projectPath + Path.DirectorySeparatorChar + "Contact.scr";
                //string path = @"D:\SideDB\MailApp\Contact.scr";
                ds1.ReadXml(path);
                dt = ds1.Tables[0];

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (email == dt.Rows[i]["Email"].ToString())
                    {
                        dt.Rows[i]["Name"] = txtName.Text;
                        dt.Rows[i]["Company"] = txtCompany.Text;
                        dt.Rows[i]["Telephone"] = txtPhone.Text;
                        dt.Rows[i]["Email"] = txtEmail.Text;
                    }
                }
            }
        }

        public bool ValidateEmailAddress(string emailAddress)
        {
            try
            {
                string TextToValidate = emailAddress;
                //                Regex expression = new Regex(@"\w+@[a-zA-Z_]+?\.[a-zA-
                //                Z]{2,3}");
                //                // test email address with expression
                //                if (expression.IsMatch(TextToValidate))
                //                {
                //                    // is valid email address
                //                    return true;
                //                }
                //                else
                //                {
                //                    // is not valid email address
                //                    return false;
                //                }
                Regex emailregex = new Regex("(?<user>[^@]+)@(?<host>.+)");
                Match m = emailregex.Match(TextToValidate);
                if (m.Success)
                {
                    // success,, so do something;
                    return true;
                }
                else
                {
                    // didn't manage to find the regular expression
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }


        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!Mod)
            {
                string path = _projectPath + Path.DirectorySeparatorChar + "Contact.scr";
                //string path = @"D:\SideDB\MailApp\Contact.scr";
                DataTable temp = new DataTable();
                DataSet ds1 = new DataSet();
                if (File.Exists(path))
                {
                    ds1.ReadXml(path);
                    temp = ds1.Tables[0];
                }

                for (int i = 0; i < temp.Rows.Count; i++)
                {
                    DataRow row1 = dt.NewRow();
                    row1["Name"] = temp.Rows[i]["Name"].ToString();
                    row1["Company"] = temp.Rows[i]["Company"].ToString();
                    row1["Telephone"] = temp.Rows[i]["Telephone"].ToString();
                    row1["Email"] = temp.Rows[i]["Email"].ToString();

                    dt.Rows.Add(row1);
                }

                ds.Tables.Add(dt);
                ds.WriteXml(path);
            }
            else
            {
                string path = _projectPath + Path.DirectorySeparatorChar + "Contact.scr";
                //string path = @"D:\SideDB\MailApp\Contact.scr";
                //ds.Tables.Add(dt);
                //ds.WriteXml(path);
                //Mod = false;
                if (dt.Rows.Count > 0)
                {
                    DataTable temp = dt.Copy();
                    DataSet ds2 = new DataSet();
                    ds2.Tables.Add(temp);
                    ds2.WriteXml(path);
                    Mod = false;
                }
                else
                {

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtCompany.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
        }

        private void AddContactMail_Load(object sender, EventArgs e)
        {
            txtName.Text = _Name;
            txtCompany.Text = _Company;
            txtPhone.Text = _Telephone;
            txtEmail.Text = _Email;
        }
    }
}